﻿using IndustrialControlLibrary.Utils;
using Modbus.Device;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IndustrialControlLibrary
{
    class TagsManager
    {
        private byte slaveAddress;
        private char addressCode;
        private ushort registerAdderss;
        private BigOrLittle bigOrLittle;
        private StoreByteCategory byteCategory;
        // 观察者模式，事件绑定显示数据函数
        public event Action<string, string> toDisplay;

        public dynamic modbusMaster { get; set; }
        public List<Control> controlList { get; set; }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="bl"></param>
        /// <param name="sbc"></param>
        public TagsManager(BigOrLittle bl, StoreByteCategory sbc)
        {
            bigOrLittle = bl;
            byteCategory = sbc;
        }

        /// <summary>
        /// ushort 转 byte[]
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public byte[] ToBuffer(ushort value)
        {
            byte[] ret = new byte[2];
            if (bigOrLittle == BigOrLittle.BigEndian)
            {
                ret[0] = (byte)((0xff00 & value) >> 8);
                ret[1] = (byte)(0xff & value);
            }
            else
            {
                ret[0] = (byte)(0xff & value);
                ret[1] = (byte)((0xff00 & value) >> 8);
            }
            return ret;
        }

        /// <summary>
        /// 获取数据函数
        /// </summary>
        public void GetData()
        {
            // 先单个获取，todo:后面整体获取
            foreach (dynamic cotrol in controlList)
            {
                string[] _stringAddress = cotrol.Address.Split(':');
                slaveAddress = Convert.ToByte(_stringAddress[0]);
                addressCode = _stringAddress[1][0];
                registerAdderss = (ushort)(Convert.ToInt32(_stringAddress[1].Remove(0, 1)) - 1);
                switch (addressCode)
                {
                    case '0':
                        bool[] result = modbusMaster.ReadCoils(slaveAddress, registerAdderss, 1);
                        toDisplay(cotrol.Name, result[0].ToString());
                        break;
                    case '1':
                        bool[] result1 = modbusMaster.ReadInputs(slaveAddress, registerAdderss, 1);
                        toDisplay(cotrol.Name, result1[0].ToString());
                        break;
                    case '3':
                        ushort[] result3 = modbusMaster.ReadInputRegisters(slaveAddress, registerAdderss, 1);
                        if (cotrol.NumType == NumType.short_16)
                        {
                            var ret = ToBuffer(result3[0]);
                            var ret2 = Transform.ToInt16(ret, 0);
                            toDisplay(cotrol.Name, ret2.ToString());
                        }
                        else
                        {
                            toDisplay(cotrol.Name, result3[0].ToString());
                        }
                        break;
                    case '4':
                        ushort[] result4 = modbusMaster.ReadHoldingRegisters(slaveAddress, registerAdderss, 1);
                        if (cotrol.NumType == NumType.short_16)
                        {
                            var ret = ToBuffer(result4[0]);
                            var ret2 = Transform.ToInt16(ret, 0);
                            toDisplay(cotrol.Name, ret2.ToString());
                        }
                        else
                        {
                            toDisplay(cotrol.Name, result4[0].ToString());
                        }
                        break;
                    default:
                        MessageBox.Show("地址码：" + addressCode + "不正确");
                        break;
                }

            }//end foreach
        }


    }
}
